{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a273ea8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %load exercise.py\n",
    "import os\n",
    "import re\n",
    "import pandas as pd\n",
    "\n",
    "os.chdir('../../lecture-python/raw-data')\n",
    "\n",
    "cbond = pd.read_excel('cbond-interest-info.xlsx')\n",
    "cbond.columns = ['code','name','interest-info']\n",
    "cbond.drop(index=[0],inplace = True)\n",
    "\n",
    "\n",
    "year=re.compile(r'(.*?)第[一二三四五六]年(.*?)([\\d\\.]+)%')\n",
    "#票面利率0.0X%/年类型\n",
    "per_year=re.compile(r'(.*?)([\\d\\.]+)%(.*?)')\n",
    "\n",
    "code_name=cbond.loc[:,[\"code\",\"name\"]]\n",
    "info=cbond.loc[:,[\"interest-info\"]]\n",
    "data_list=[]\n",
    "data_list.append([''])\n",
    "\n",
    "for row in range(len(info)):\n",
    "    string=str(info.iloc[row,0])\n",
    "    item=re.findall(year,string)\n",
    "    data=[]\n",
    "    if(item==[]):\n",
    "        item=re.findall(per_year,string)\n",
    "        for i in range(6):\n",
    "            data.append(item[0][1]+\"%\")\n",
    "    else:\n",
    "        for i in range(len(item)):\n",
    "            rate=item[i][2]+'%'\n",
    "            data.append(rate)\n",
    "    data_list.append(data)\n",
    "\n",
    "data_frame=pd.DataFrame(data_list)\n",
    "data_frame.columns = ['第一年','第二年','第三年','第四年','第五年','第六年']\n",
    "data_frame=pd.concat([code_name,data_frame],axis=1)\n",
    "data_frame.drop(index=[0],inplace = True)\n",
    "print(data_frame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ae13fa4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "361191d1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
